﻿Imports System.IO
Imports System.ComponentModel
Imports System.Threading
Public Class WinMain
    Private Sub WinMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim allDrives As DriveInfo() = DriveInfo.GetDrives()
        For Each drive As DriveInfo In allDrives
            cboDrive.Items.Add(drive.Name)
        Next
        cboDrive.SelectedIndex = 0
    End Sub
    Private scanThread As Thread
    Private _isPaused As Boolean = False
    Private _isStopped As Boolean = False
    Private _waitHandle As New AutoResetEvent(False)
    Private _lockObject As New Object()

    Private Sub btnStartScan_Click(sender As Object, e As EventArgs) Handles btnStartScan.Click
        ' 重置状态
        _isPaused = False
        _isStopped = False
        lblFilePath.Text = "未开始"

        ' 创建并启动扫描线程
        Dim selectedDrive As String = cboDrive.SelectedItem.ToString()
        scanThread = New Thread(Sub() ScanDrive(selectedDrive))
        scanThread.Start()
    End Sub

    Private Sub btnPauseOrContinue_Click(sender As Object, e As EventArgs) Handles btnPauseOrContinue.Click
        SyncLock _lockObject
            _isPaused = Not _isPaused

            If _isPaused Then
                btnPauseOrContinue.Text = "继续"
            Else
                btnPauseOrContinue.Text = "暂停"
                _waitHandle.Set() ' 继续扫描
            End If
        End SyncLock
    End Sub

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        SyncLock _lockObject
            _isStopped = True
            _waitHandle.Set() ' 发出停止信号
        End SyncLock

        If Not scanThread Is Nothing AndAlso scanThread.IsAlive Then
            scanThread.Join()
        End If
    End Sub

    Private Sub ScanDrive(driveLetter As String)
        Try
            Dim rootDirectory As String = driveLetter
            ScanDirectory(rootDirectory)
        Catch ex As Exception
            MessageBox.Show("扫描磁盘时出现错误：" & ex.Message)
        End Try
    End Sub

    Private Sub ScanDirectory(directoryPath As String)
        Dim directoriesToScan As New Stack(Of String)
        directoriesToScan.Push(directoryPath)

        While directoriesToScan.Count > 0
            Dim currentDirectory As String = directoriesToScan.Pop()

            Try
                Dim files As String() = Directory.GetFiles(currentDirectory)
                For Each file As String In files
                    ' 检查是否需要暂停扫描
                    SyncLock _lockObject
                        While _isPaused AndAlso Not _isStopped
                            _waitHandle.WaitOne() ' 等待继续指令
                        End While
                    End SyncLock

                    ' 如果需要终止扫描，则退出循环
                    SyncLock _lockObject
                        If _isStopped Then
                            Exit Sub ' 立即退出扫描
                        End If
                    End SyncLock

                    ' 显示文件路径到 Label 组件上
                    UpdateLabel(file)
                Next

                Dim subDirectories As String() = Directory.GetDirectories(currentDirectory)
                For Each subDir As String In subDirectories
                    directoriesToScan.Push(subDir)
                Next
            Catch ex As Exception
                ' 如果出现异常，记录并继续下一个目录
                Console.WriteLine("访问目录时出现错误：" & ex.Message)
            End Try
        End While
    End Sub

    Private Sub UpdateLabel(file As String)
        If lblFilePath.InvokeRequired Then
            Dim updateLabelCallback As New Action(Of String)(AddressOf UpdateLabel)
            lblFilePath.Invoke(updateLabelCallback, file)
        Else
            lblFilePath.Text = file
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        ' 程序关闭时，确保停止扫描线程
        If Not scanThread Is Nothing AndAlso scanThread.IsAlive Then
            SyncLock _lockObject
                _isStopped = True
                _waitHandle.Set() ' 发出停止信号
            End SyncLock

            scanThread.Join()
        End If
    End Sub

    Private Sub aboutItem_Click(sender As Object, e As EventArgs) Handles aboutItem.Click
        About.Show()
    End Sub
End Class
